package Cliente.ventanas;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Cliente.TCPCliente;

public class ButtonPanel extends JPanel implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton cambiarTrenMas;
	private JButton cambiarTrenMenos;
	private JButton actualizar;
	private JButton salir;
	MainWindow main;
	TCPCliente tcp;
	
	public ButtonPanel(MainWindow main, TCPCliente tcp){
		
		
		cambiarTrenMas = new JButton(">");
		cambiarTrenMenos = new JButton("<");
		
		this.tcp = tcp;
		this.main = main;
		JLayeredPane layer = new JLayeredPane();
		actualizar = new JButton(new ImageIcon("src/Img/act.png"));
		salir = new JButton(new ImageIcon("src/Img/salir.png"));
		JLabel img = new JLabel(new ImageIcon("src/Img/fondopeq.jpg"));
		
		salir.addActionListener(this);
		actualizar.addActionListener(this);
		
		this.setVisible(true);
		this.setSize(500,100);
		this.setLayout(null);
		
		layer.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
		layer.setBounds(0,0,495,100);
		
		cambiarTrenMas.setBounds(300, 70, 50, 30);
		cambiarTrenMenos.setBounds(250,  70, 50, 30);

		actualizar.setBounds(20, 15, 70, 70);
		salir.setBounds(410, 15,70, 70);
		img.setBounds(0, 0, 500, 100);
		
		this.add(layer);
		
		layer.add(img, new Integer(0));
		layer.add(cambiarTrenMas, new Integer(1));
		layer.add(cambiarTrenMenos, new Integer(1));
		layer.add(actualizar, new Integer(1));
		layer.add(salir, new Integer(1));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==salir){
			try {
				SALIR();
				main.dispose();
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}else if(e.getSource()==actualizar){
			actualizar();
		}
	}
	
	public String SALIR() throws IOException{
//		System.out.println("salir");
		tcp.sentMessage("SALIR\n");
//		System.out.println(tcp.receiveMessage());
		return null;
		
	}
	public void actualizar(){
		try {
			tcp.sentMessage("ACTUALIZAR\n");
			String recibido=tcp.receiveMessage();
			JOptionPane.showMessageDialog(this, recibido);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
